package main;

/**
 * 给定一个字符串 s，找到 s 中最长的回文子串。你可以假设 s 的最大长度为1000。
 * <p>
 * 示例 1：
 * 输入: "babad"
 * 输出: "bab"
 * 注意: "aba"也是一个有效答案。
 * <p>
 * 示例 2：
 * 输入: "cbbd"
 * 输出: "bb"
 */
public class LeetCode5 {
    public String longestPalindrome(String s) {  // 算法超时了
        String substring;
        String result = "";
        int max = 0;
        int i = s.length() - 1;
        if (s.length() == 0)
            return "";
        result = s.substring(0, 1);
        for (; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                substring = s.substring(j, i + 1);
                if (isPalindrome(substring.toCharArray(), 0, substring.length() - 1)) {
                    if (max < substring.length()) {
                        max = substring.length();
                        result = substring;
                    }
                } else {
                    continue;
                }
            }
        }
        return result;
    }

    public boolean isPalindrome(String s) {
        int length = s.length();
        char[] arr = s.toCharArray();
        for (int i = 0; i < length / 2; i++) {
            if (arr[i] != arr[length - i - 1]) {
                return false;
            }
        }
        return true;
    }

    public boolean isPalindrome(char[] arr, int head, int tail) {
        if ((head == tail) || (Math.abs(head - tail) == 1 && arr[head] == arr[tail])) {
            return true;
        }
        if (arr[head] != arr[tail])
            return false;

        return isPalindrome(arr, ++head, --tail);
    }

    public static void main(String[] args) {
        LeetCode5 leetCode5 = new LeetCode5();
        String s = "whdqcudjpisufnrtsyupwtnnbsvfptrcgvobbjglmpynebblpigaflpbezjvjgbmofejyjssdhbgghgrhzuplbeptpaecfdanhlylgusptlgobkqnulxvnwuzwauewcplnvcwowmbxxnhsdmgxtvbfgnuqdpxennqglgmspbagvmjcmzmbsuacxlqfxjggrwsnbblnnwisvmpwwhomyjylbtedzrptejjsaiqzprnadkjxeqfdpkddmbzokkegtypxaafodjdwirynzurzkjzrkufsokhcdkajwmqvhcbzcnysrbsfxhfvtodqabvbuosxtonbpmgoemcgkudandrioncjigbyizekiakmrfjvezuzddjxqyevyenuebfwugqelxwpirsoyixowcmtgosuggrkdciehktojageynqkazsqxraimeopcsjxcdtzhlbvtlvzytgblwkmbfwmggrkpioeofkrmfdgfwknrbaimhefpzckrzwdvddhdqujffwvtvfyjlimkljrsnnhudyejcrtrwvtsbkxaplchgbikscfcbhovlepdojmqybzhbiionyjxqsmquehkhzdiawfxunguhqhkxqdiiwsbuhosebxrpcstpklukjcsnnzpbylzaoyrmyjatuovmaqiwfdfwyhugbeehdzeozdrvcvghekusiahfxhlzclhbegdnvkzeoafodnqbtanfwixjzirnoaiqamjgkcapeopbzbgtxsjhqurbpbuduqjziznblrhxbydxsmtjdfeepntijqpkuwmqezkhnkwbvwgnkxmkyhlbfuwaslmjzlhocsgtoujabbexvxweigplmlewumcone";
        long before = System.currentTimeMillis();
        System.out.println(leetCode5.isPalindrome(s.toCharArray(),0,s.length()-1));
//        long before = System.currentTimeMillis();
//        String result = leetCode5.longestPalindrome(s);
        System.out.println(System.currentTimeMillis() - before);
        long before1 = System.currentTimeMillis();
        System.out.println(leetCode5.isPalindrome(s));
        System.out.println(System.currentTimeMillis() - before1);

//        System.out.println(result);


    }
}

